import json
import sys
sys.path.append("../")

from services.ZhangTingService import *
from frameworks.utils.RedisUtil import *
from model.StockBaseInfo import *
from frameworks.utils.PrettyTableUtil import *
from frameworks.cores.LoadClass import *
import time
import pandas as pd
import os

loader = LoadClass()
redis = RedisUtil()

def checkzhangting():
    zhangtingService = ZhangTingService()
    stocks = zhangtingService.getOneZhangting()
    zhangtingcode = []
    for row in stocks:
        code = row["code"][2:]
        if row["code"][2:3] == "3" or row["code"][2:4] == "68":
            continue
        ck1 = "zhangting_cache_" + code
        data = redis.vget(ck1)
        if data != None:
            olddata = json.loads(data)
            df = pd.DataFrame(olddata)
            for index,lastrow in df.iterrows():
                close = float(lastrow["close"])
                zhangtingjia = float(lastrow["zhangtingjia"])
                if close == zhangtingjia:
                    if code not in zhangtingcode:
                        zhangtingcode.append(code)
    return zhangtingcode


def monitor_zhangting():
    zhangtingService = ZhangTingService()
    stocks = zhangtingService.getOneZhangting()
    zhangtingcode = checkzhangting()
    print(zhangtingcode)
    for row in stocks:
        code = row["code"][2:]
        if row["code"][2:3] == "3" or row["code"][2:4] == "68":
            continue
        ck1 = "zhangting_cache_" + code
        data = redis.vget(ck1)
        if data != None:
            olddata = json.loads(data)
            df = pd.DataFrame(olddata)
            lastrow = df[-1:]
            close = float(lastrow["close"].iloc[0])
            zhangtingjia = float(lastrow["zhangtingjia"].iloc[0])
            codename = lastrow["codename"].iloc[0]
            if close == zhangtingjia:
                if code not in zhangtingcode:
                    zhangtingcode.append(code)
                    os.system("python3 /home/data/wwwroot/gusto/scripts/send_sto_msg.py 'xx88" + code + "xxting'")
                    #os.system("py send_sto_msg.py 'xx88"+ code + "xxting'")
            else:
                if code in zhangtingcode:
                    os.system("python3 /home/data/wwwroot/gusto/scripts/send_sto_msg.py 'xx88" + code + "xxopen'")
                    #os.system("py send_sto_msg.py 'xx88"+ code + "xxopen'")
        break
monitor_zhangting()
